﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using suirui.ZhuMu.Common;
using suirui.ZhuMu.Model;
using suirui.ZhuMu.Entity;
using suirui.ZhuMu.Entity.Entities;
using suirui.ZhuMu.Services;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using suirui.ZhuMu.WebCore;

namespace suirui.ZhuMu.MJGames.Controllers
{
    public class RoomController : BaseController
    {
        private readonly IRoomService _RoomService;

        public RoomController(IRoomService healthSerivce)
        {
            this._RoomService = healthSerivce ?? throw new ArgumentNullException(nameof(healthSerivce));
        }

        /// <summary>
        /// 获取房间信息列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public Task<APIResult<PageData<Room>>> GetRoomList(GetParamsDto paramsDto)
        {
            return _RoomService.GetRoomList(paramsDto);
        }

        /// <summary>
        /// 通过Id获取单个房间的信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<Room>> GetRoomById(long Id)
        {
            if (Id <= 0)
            {
                return new APIResult<Room>() { Code = ErrorCode.FormatError, Message = "Id不能小于0" };
            }
            return await _RoomService.GetRoomById(Id);
        }

        /// <summary>
        /// 注册房间信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [PreventDuplicationRequest]
        public async Task<APIResult> AddRoom(Room health)
        {
            if (!CheckInvalid(health))
            {
                return new APIResult() { Code = ErrorCode.FormatError, Message = "房间参数不能为null" };
            }
            return await _RoomService.AddRoom(health);
        }

        /// <summary>
        /// 更新房间信息
        /// </summary>
        /// <param name="health"></param>
        [HttpPut]
        public async Task<APIResult> UpdateRoom(Room health)
        {
            if (!CheckInvalid(health))
            {
                return new APIResult() { Code = ErrorCode.FormatError, Message = "房间参数不能为null" };
            }
            return await _RoomService.UpdateRoom(health);
        }

        /// <summary>
        /// 删除房间信息
        /// </summary>
        /// <param name="Id"></param>
        [HttpDelete]
        public async Task<APIResult> DeleteRoom(long Id)
        {
            if (Id <= 0)
            {
                return new APIResult() { Code = ErrorCode.FormatError, Message = "Id不能小于或等于0" };
            }
            return await _RoomService.DeleteRoom(Id);
        }


        private bool CheckInvalid(Room user)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            return true;
        }

    }
}
